package com.atguigu.connection.util;

import com.alibaba.druid.pool.DruidDataSourceFactory;
import com.mchange.v2.c3p0.ComboPooledDataSource;
import org.apache.commons.dbcp2.BasicDataSource;
import org.apache.commons.dbcp2.BasicDataSourceFactory;
import org.apache.commons.dbutils.DbUtils;

import javax.sql.DataSource;
import java.io.InputStream;
import java.sql.*;
import java.util.Properties;

/**
 * @author admin
 */
public class JDBCUtil {

    /**
     * 使用 C3P0 数据库连接池技术
     *
     * @return
     * @throws SQLException
     */
    private static ComboPooledDataSource comboPooledDataSource = new ComboPooledDataSource("helloc3p0");

    public static Connection getConnection1() throws SQLException {
        Connection connection = comboPooledDataSource.getConnection();
        return connection;
    }

    /**
     * 使用 DBCP 数据库连接池技术
     *
     * @return
     * @throws Exception
     */
    private static DataSource source;

    static {
        try {
            Properties properties = new Properties();
            InputStream inputStream = ClassLoader.getSystemClassLoader().getResourceAsStream("dbcp.properties");
            properties.load(inputStream);

            // 创建 DBCP 数据库连接池
            BasicDataSource source = BasicDataSourceFactory.createDataSource(properties);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static Connection getConnection2() throws Exception {

        Connection connection = source.getConnection();

        return connection;
    }

    /**
     * Druid 数据库连接池
     *
     * @return
     * @throws Exception
     */
    private static DataSource source1;

    static {
        try {
            Properties properties = new Properties();
            InputStream inputStream = ClassLoader.getSystemClassLoader().getResourceAsStream("druid.properties");
            properties.load(inputStream);

            source1 = DruidDataSourceFactory.createDataSource(properties);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static Connection getConnection3() throws Exception {

        Connection connection = source1.getConnection();

        return connection;
    }

    /**
     * 使用 dbutils.jar 中提供的 DBUtils工具类，实现资源的关闭
     */
    public static void closeResource(Connection connection, Statement statement, ResultSet resultSet){
        // try {
        //     DbUtils.close(connection);
        // } catch (SQLException throwables) {
        //     throwables.printStackTrace();
        // }
        // try {
        //     DbUtils.close(statement);
        // } catch (SQLException throwables) {
        //     throwables.printStackTrace();
        // }
        // try {
        //     DbUtils.close(resultSet);
        // } catch (SQLException throwables) {
        //     throwables.printStackTrace();
        // }

        DbUtils.closeQuietly(connection);
        DbUtils.closeQuietly(statement);
        DbUtils.closeQuietly(resultSet);
    }
}
